<?php

namespace App\Admin\Forms;

use Dcat\Admin\Widgets\Form;

class Setting extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        admin_setting($input);

        return $this
            ->response()
            ->success('Processed successfully.')
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->disableResetButton();
        $this->confirm('你确定要提交设置吗？', '部分设置提交之后需要重新刷新一下浏览器才能生效');
        $this->text('web_name', '网站名称');
        $this->url('web_url', '网站地址')->required()->rules('url')
            ->help('正确填写网址，并且必须以/结尾，否则会导致LOGO无法显示');
        $this->image('logo', '网站LOGO')
            ->accept('jpg,png,gif,jpeg')
            ->override()
            ->maxSize(512)
            ->required()
            ->autoUpload()
            ->help('大小不要超过512k');
        $this->radio('horizontal_menu', '菜单位置')
            ->options([0 => '侧栏', 1 => '顶栏']);
        $this->radio('style_type', '网站风格')
            ->options([1 => '旧版', 2 => '大字版']);
        $this->radio('sidebar_style', '侧栏颜色')
            ->options(['light' => '白色', 'dark' => '黑色', 'primary' => '彩色']);
        $this->radio('login_theme', '登录页样式')
            ->options(['big_picture' => '大图', 'simple' => '简单']);
        $this->image('logo_bg', '登录页背景图')
            ->accept('jpg,png,gif,jpeg')
            ->override()
            ->maxSize(1024)->autoUpload()
            ->help('大小不要超过512k，仅在登录页为大图模式下生效');
        $this->file('file', '上传文件')->override();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
//        admin_settings()方法的使用
//        1. 如果参数是key和value，那么就是读取的作用，例如：admin_settings(key, value)
//        2. 如果参数是数组，那么就是保存的作用，例如：admin_settings([key => value])
        return [
            'web_name' => admin_setting('web_name', 'NXCASE案件管理系统'),
            'web_url' => admin_setting('web_url', 'https://nx.tt'),
            'logo' => admin_setting('logo'),
            'horizontal_menu' => admin_setting('horizontal_menu', 0),
            'style_type' => admin_setting('style_type', 2),
            'sidebar_style' => admin_setting('sidebar_style', 'dark'),
            'login_theme' => admin_setting('login_theme'),
            'logo_bg' => admin_setting('logo_bg'),
            'file' => admin_setting('file'),
        ];
    }
}
